from marshmallow import EXCLUDE
from webargs import core
from webargs.tornadoparser import WebArgsTornadoMultiDictProxy as DictProxy
from webargs.tornadoparser import TornadoParser as Parser

# 客户端可能有些不规范的行为
# 比如：`?key=`  在 query 中传递了参数名但是没有值
# 所以服务端需要统一处理下这个问题


class IgnoreBlankMultiDictProxy(DictProxy):
    def __getitem__(self, key):
        value = super().__getitem__(key)
        if value == '':
            return core.missing
        return value


class TornadoParser(Parser):
    DEFAULT_UNKNOWN_BY_LOCATION = {'query': EXCLUDE}

    def load_querystring(self, req, schema):
        return IgnoreBlankMultiDictProxy(req.query_arguments, schema)


parser = TornadoParser()
use_args = parser.use_args
use_kwargs = parser.use_kwargs
